package com.jz.banner.screen.net.observer

import android.annotation.SuppressLint
import android.app.Activity
import com.jz.banner.screen.bean.BaseBean
import com.jz.banner.screen.utils.CommUtils
import com.jz.banner.screen.utils.ToastUtil
import com.jz.banner.screen.widget.statueslayoutmanager.StatusLayoutManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * 创建者     Noel
 * 创建时间   2018/1/20 16:55
 * 描述	       对于网络请求是否需要弹出进度对话框
 */

abstract class StatusLayoutObserver<T>(
    activity: Activity,
    private var mStatusLayoutManager: StatusLayoutManager?,
    private var delayTime: Long = 0
) : BaseObserver<T>(activity) {


    override fun onSubscribe(d: Disposable) {
        mStatusLayoutManager?.showLoadingLayout()
        mDisposable = d
    }

    override fun onError(e: Throwable) {
        CommUtils.println("onError:", e.toString())
        ToastUtil.showToast(e.message)
        mStatusLayoutManager?.showErrorLayout()
        if (!isHandleRequestMultiData()) {
            mDisposable!!.dispose()
        }
    }

    /*成功请求多个数据完毕后*/
    @SuppressLint("CheckResult")
    override fun onComplete() {

        Observable.just("")
            //延时两秒，第一个参数是数值，第二个参数是事件单位
            .delay(delayTime, TimeUnit.MILLISECONDS)
            // Run on a background thread
            .subscribeOn(Schedulers.io())
            // Be notified on the main thread
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                mStatusLayoutManager?.showSuccessLayout()
            } //这里的观察者依然不重要
    }

    override fun dataIsNull(t: BaseBean<out T>) {
        //super.dataIsNull(t)
        if (!isHandleRequestMultiData()) {
            mStatusLayoutManager?.showEmptyLayout()
            mDisposable!!.dispose()
        }
    }
}
